package com.rise.remi.main;

import com.rise.remi.controller.Controller;
import com.rise.remi.core.Core_v1;
import com.rise.remi.core.Global;
import com.rise.remi.network.NetworkMessage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;

/**
 *
 * @author remi
 * @version 0.2
 */
public class Main extends JFrame implements ActionListener {

    public static Main game;
    //public static Core_v1 core;
    private static JTextField chatField;
    private static JPanel panel;
    private static boolean enabled = false;
    private static JTextArea text;
    private static Core_v1 core;
    private Controller controller = new Controller(false);
    private Controller chatController = new Controller(true);
    private static JPanel loginScreen;
    //UI elements for login screen
    private static JButton server = new JButton("Host"),
            client = new JButton("Connect");
    private static JTextField name = new JTextField("Player"),
            ip = new JTextField("127.0.0.1");
    private static JCheckBox localhost = new JCheckBox("localhost");

    public Main(String title) {
        Core_v1.getInstance();
        setTitle(title);
        setBounds(10, 10, Global.windowWidth, Global.windowHeight);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        addKeyListener(controller);

        this.setLayout(new BorderLayout());

        chatField = new JTextField("");
        panel = new JPanel(new BorderLayout());
        text = new JTextArea();
        text.setEditable(false);
        text.setEnabled(false);
        text.setDisabledTextColor(Color.BLACK);
        panel.setPreferredSize(new Dimension(0, 100));

        JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        DefaultCaret caret = (DefaultCaret) text.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        panel.add(chatField, BorderLayout.SOUTH);
        panel.add(scroll);
        chatField.setEnabled(enabled);
        text.setLineWrap(true);
        chatField.addKeyListener(chatController);
        add(panel, BorderLayout.SOUTH);
        panel.setVisible(false);

        //creating login screen
        loginScreen = new JPanel();
        loginScreen.setLayout(null);

        int yOffset = 80;
        name.setBounds(160, 100 + yOffset, 320, 24);
        ip.setBounds(160, 130 + yOffset, 320, 24);
        server.setBounds(160, 160 + yOffset, 160, 32);
        client.setBounds(320, 160 + yOffset, 160, 32);

        loginScreen.add(name);
        loginScreen.add(ip);
        loginScreen.add(server);
        loginScreen.add(client);

        server.addActionListener(this);
        client.addActionListener(this);
    }

    public static void main(String[] args) throws Exception {
        game = new Main("Game");
        core = Core_v1.getInstance();

        game.add(loginScreen, BorderLayout.CENTER);
        loginScreen.setVisible(true);

        //core.init();
        //game.add(core);
        game.setVisible(true);
    }

    public static void toggleChat() {
        enabled = !enabled;
        chatField.setEnabled(enabled);
        if (!enabled) {
            if (chatField.getText().length() > 0) {
                text.append(Core_v1.getInstance().getPlayer().getName() + ": " + chatField.getText() + "\n\r");
                Core_v1.getInstance().getNetwork().sendData(new NetworkMessage(NetworkMessage.CHAT_MESSAGE, chatField.getText() + "\n\r"));
            }
            chatField.setText(null);
            game.requestFocus();
        } else {
            chatField.requestFocus();
        }
    }

    public static void addToChat(String msg) {
        text.setBackground(Color.GRAY);
        text.append(msg);
        text.setBackground(Color.WHITE);
    }

    public static Core_v1 getCore() {
        return Core_v1.getInstance();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == server) {
            core.init(ip.getText(), name.getText(), true);
            game.add(core);
            loginScreen.setVisible(false);
            panel.setVisible(true);
            game.remove(loginScreen);
        }
        if (e.getSource() == client) {
            core.init(ip.getText(), name.getText(), false);
            game.add(core);
            loginScreen.setVisible(false);
            panel.setVisible(true);
            game.remove(loginScreen);
        }
    }
}
